<template>
  <div class="articleContainer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章管理</span>
      </div>
      <el-table
        :cell-style="{textAlign:'center'}"
        :data="articleData"
        :header-cell-style="{textAlign:'center'}"
        style="width: 100%"
      >
        <el-table-column
          label="作者昵称"
          prop="authorName"
        />
        <el-table-column
          label="文章类型"
          prop="category"
        />
        <el-table-column
          label="频道名称"
          prop="channelName"
        />
        <el-table-column
          label="文章标题"
          prop="title"
        />
        <el-table-column
          label="文章图片"
          prop="image"
        >
          <template slot-scope="scope">
            <el-popover placement="right" title="" trigger="click">
              <img :src="scope.row.image"  style="height: 200px;width: 200px"/>
              <img slot="reference" :src="scope.row.image" :alt="scope.row.image" style="height: 50px;width: 50px">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          label="点赞数"
          prop="likes"
        />
        <el-table-column
          label="评论数"
          prop="comment"
        />
        <el-table-column
          label="阅读数"
          prop="views"
        />
        <el-table-column
          label="状态"
          prop="status"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status===1 ?'success':'danger'">{{
                scope.row.status === 1 ? '已发布' : '未发布'
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          :render-header="renderHeader"
        >
          <template slot-scope="scope">
            <el-button style="color: red" size="mini" type="text" @click="del(scope.row)">删除</el-button>
            <el-button size="mini" type="text" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        style="margin: 20px 0;float: right"
        v-show="queryParams.total>0"
        :total="queryParams.total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getData"
      />
    </el-card>
  </div>
</template>

<script>
import {addArticleList, delArticleList, getArticleList, queryArticleList, updateArticleList} from "@/api/article";

export default {
  name: 'Article',
  data() {
    return {
      fileList:[],
      //分页查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      articleData: [],
      id: ""
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //获取文章数据
    getData() {
      getArticleList().then(res => {
        this.articleData = res.data.records
        this.articleData.forEach(item => item.image = item.images.split(',')[0])
        this.queryParams.total = res.data.total
      })
    },
    //跳转到编辑文章页面
    edit(row) {
      this.$router.push({
        name:"editArticle",
        query:{
          id:row.id
        }
      })
    },
    //跳转到编辑文章页面
    handleAddItem(){
      this.$router.push('/addArticle')
    },
    renderHeader(h) {
      return (
        <div>操作
          <el-button type="success" size='mini' onClick={() => this.handleAddItem()}
                     style="margin-left:20px">添加</el-button>
        </div>
      )
    },

    //  删除文章数据
    del(row) {
      const id = row.id
      this.$confirm('您确定删除这条数据么', '提示', {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delArticleList(id).then(() => {
          this.$message.success('删除成功')
          this.getData()
        })
      }).catch(function () {
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.articleContainer {
  padding: 20px;
}
</style>
